/*
 * @Author: Jimmy
 * @LastModifiedBy: Jimmy
 * @Date: 2022-03-27 22:53:57
 * @LastEditTime: 2025-04-18 20:59:34
 * @FilePath: /lottery-box-react-master/src/routes/index.tsx
 */
import React, { FC, useEffect } from 'react';
import { staticRoutes } from './config';
import { Route, Routes, useNavigate } from 'react-router-dom';

// 路由装饰器
const RouteDecorator = (props: { route: any }) => {
  const { route } = props;
  const navigate = useNavigate();

  useEffect(() => {
    // 鉴权路由守卫
    // if (route.meta?.requireAuth) {
    //   if (!isLogin()) {
    //     navigate('/login', { state: { redirect: route.pathname } });
    //   }
    // }
    // 自定义路由守卫
    route.beforeCreate && route.beforeCreate(route);
    return () => route.beforeDestroy && route.beforeDestroy(route);
  }, [route]);

  return <route.component />;
};

const RouterComponent: FC = () => (
  <Routes>
    {staticRoutes.map((route: any) => (
      <Route
        key={route.pathname}
        path={route.pathname}
        element={<RouteDecorator route={route} />}
      />
    ))}
  </Routes>
);

export default RouterComponent;
